<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>数据挖掘 | Data Mining Course</title>
    <link rel="stylesheet" href="../../css/main.css" />
    <link rel="stylesheet" href="../../css/tabs.css" />
    <script src="../../js/uikit.js"></script>
    <link href="../../css/prism.css" rel="stylesheet" />
    <script src="../../js/prism.js"></script>
  </head>

  <body>
    <div
      data-uk-sticky="animation: uk-animation-slide-top; sel-target: .uk-navbar-container; cls-active: uk-navbar-sticky; cls-inactive: uk-navbar-transparent; top: 300"
    >
      <nav class="uk-navbar-container pt-2 pb-2">
        <div class="uk-container">
          <div data-uk-navbar>
            <div class="uk-navbar-left">
              <a
                class="uk-navbar-item uk-logo uk-visible@m"
                href="../../index.html"
                >数据挖掘课程</a
              >
              <a
                class="uk-navbar-tuk-containeroggle uk-hidden@m"
                href="#offcanvas-docs"
                data-uk-toggle
                ><span data-uk-navbar-toggle-icon></span>
                <span class="uk-margin-small-left">目录</span></a
              >
              <ul class="uk-navbar-nav uk-visible@m">
                <li><a href="../index.html">文档</a></li>
              </ul>
            </div>
            <div class="uk-navbar-center uk-hidden@m">
              <a class="uk-navbar-item uk-logo" href="../../index.html"
                >数据挖掘课程</a
              >
            </div>
          </div>
        </div>
      </nav>
    </div>

    <div class="uk-section">
      <div class="uk-container">
        <div class="uk-grid-large" data-uk-grid>
          <div class="sidebar-fixed-width uk-visible@m">
            <div class="sidebar-docs uk-position-fixed uk-margin-top">
              <h5>关于这门课</h5>
              <ul class="uk-nav uk-nav-default doc-nav">
                <li><a href="../../index.html">大纲</a></li>
              </ul>
              <h5>章节</h5>
              <ul class="uk-nav uk-nav-default doc-nav">
                <li><a href="../01.html">第1章 - 数据挖掘概念</a></li>
                <li><a href="../02.html">第2章 - 分类</a></li>
                <li><a href="../03.html">第3章 - 聚类</a></li>
                <li><a href="../04.html">第4章 - 关联规则</a></li>
                <li><a href="../05.html">第5章 - 日志的挖掘与应用</a></li>
                <li><a href="../06.html">第6章 - 数据挖掘应用案例</a></li>
              </ul>
              <h5>实验课</h5>
              <ul class="uk-nav uk-nav-default doc-nav">
                <li><a href="./code-01.html">01</a></li>
                <li><a href="./code-02.html">02</a></li>
                <li><a href="./code-03.html">03</a></li>
                <li class="uk-active"><a href="./code-04.html">04</a></li>
                <li><a href="./code-05.html">05</a></li>
                <li><a href="./code-06.html">06</a></li>
                <li><a href="./code-07.html">07</a></li>
                <li><a href="./code-08.html">08</a></li>
                <li><a href="./code-09.html">09</a></li>
                <li><a href="./code-10.html">10</a></li>
                <li><a href="./code-11.html">11</a></li>
                <li><a href="./code-12.html">12</a></li>
                <li><a href="./code-13.html">13</a></li>
                <li><a href="./code-14.html">14</a></li>
                <li><a href="./code-15.html">15</a></li>
                <li><a href="./code-16.html">16</a></li>
              </ul>
            </div>
          </div>
          <div class="uk-width-1-1 uk-width-expand@m">
            <article class="uk-article">
              <h3>聚类</h3>
              <div class="tabs">
                <div class="tab-2">
                  <label for="tab2-1">K-Means</label>
                  <input
                    id="tab2-1"
                    name="tabs-three"
                    type="radio"
                    checked="checked"
                  />
                  <div>
                    打开cmd依次运行<br />
                    <strong>pip install --user sklearn</strong>
                    <br />
                    和
                    <br />
                    <strong>pip install --user matplotlib</strong>
                    <br /><br />
                    <img src="../../images/lab02/0.JPG" alt="0" />
                    <br />
                    <br />
                    安装过程可能需要5分钟左右的时间
                    <hr>
<p>1. 首先我们随机创建一些二维数据作为训练集，选择二维特征数据，主要是方便可视化。代码如下：</p>
<pre><code class="language-python">import matplotlib.pyplot as plt
from sklearn.datasets.samples_generator import make_blobs

X, y = make_blobs(n_samples=1000, n_features=2, centers=[[-1, -1], [0, 0], [1, 1], [2, 2]],
                  cluster_std=[0.4, 0.2, 0.2, 0.2], random_state=9)
                  
plt.scatter(X[:, 0], X[:, 1], marker='o')
plt.show()</code></pre> 
<p>运行后会画出下面的图</p>
<img src="../../images/lab04/1.png" alt="1">

<p>2. 现在我们来用K-Means聚类方法来做聚类，首先选择k=2，代码如下：</p>
<pre><code class="language-python">from sklearn.cluster import KMeans
y_pred = KMeans(n_clusters=2, random_state=9).fit_predict(X)
plt.scatter(X[:, 0], X[:, 1], c=y_pred)
plt.show()</code></pre> 
<p>运行后会画出下面的图</p>
<img src="../../images/lab04/2.png" alt="2">
<p>然后我们用Calinski-Harabasz Index来评估聚类的效果，这个值的得分越高越好。
（请记录下面每一次更改k值后Calinski-Harabasz Index的值），从而来判定当k选取多少的时候，聚类效果最好</p>
<pre><code class="language-python">from sklearn import metrics
print("k=2时，聚类效果得分:",metrics.calinski_harabasz_score(X, y_pred))</code></pre> 


<p>3. 接下来让k=3，代码如下：</p>
<pre><code class="language-python">y_pred = KMeans(n_clusters=3, random_state=9).fit_predict(X)
plt.scatter(X[:, 0], X[:, 1], c=y_pred)
plt.show()
print("k=3时，聚类效果得分:",metrics.calinski_harabasz_score(X, y_pred))</code></pre> 
<p>运行后会画出下面的图</p>
<img src="../../images/lab04/3.png" alt="2">

<p>4. 接下来让k=4，代码如下：</p>
<pre><code class="language-python">y_pred = KMeans(n_clusters=4, random_state=9).fit_predict(X)
plt.scatter(X[:, 0], X[:, 1], c=y_pred)
plt.show()
print("k=4时，聚类效果得分:",metrics.calinski_harabasz_score(X, y_pred))</code></pre> 
<p>运行后会画出下面的图</p>
<img src="../../images/lab04/4.png" alt="4">

<p>5. 最后，运行下面的代码，看一下总的效果图：</p>
<pre><code class="language-python">from sklearn.cluster import MiniBatchKMeans
for index, k in enumerate((2, 3, 4)):
    plt.subplot(2, 2, index + 1)
    y_pred = MiniBatchKMeans(n_clusters=k, batch_size=200, random_state=9).fit_predict(X)
    score = metrics.calinski_harabasz_score(X, y_pred)
    plt.scatter(X[:, 0], X[:, 1], c=y_pred)
    plt.text(.99, .01, ('k=%d, score: %.2f' % (k, score)), transform=plt.gca().transAxes, size=10,
              horizontalalignment='right')
plt.show()</code></pre> 
<p>运行后会输出下面的画面</p>
<img src="../../images/lab04/5.png" alt="5">

                  </div>
                </div>
                <div class="tab-2">
                  <label for="tab2-2">DBSCAN</label>
                  <input id="tab2-2" name="tabs-three" type="radio" />
<div>
<p>1. 导入相关的库，并配置DBSCAN算法的代码</p>
<pre><code class="language-python">from sklearn import datasets
import numpy as np
import random
import matplotlib.pyplot as plt
import time
import copy
  
def find_neighbor(j, x, eps):
    N = list()
    for i in range(x.shape[0]):
        temp = np.sqrt(np.sum(np.square(x[j]-x[i])))  # 计算欧式距离
        if temp <= eps:
            N.append(i)
    return set(N)
  
def DBSCAN(X, eps, min_Pts):
    k = -1
    neighbor_list = []  # 用来保存每个数据的邻域
    omega_list = []  # 核心对象集合
    gama = set([x for x in range(len(X))])  # 初始时将所有点标记为未访问
    cluster = [-1 for _ in range(len(X))]  # 聚类
    for i in range(len(X)):
        neighbor_list.append(find_neighbor(i, X, eps))
        if len(neighbor_list[-1]) >= min_Pts:
            omega_list.append(i)  # 将样本加入核心对象集合
    omega_list = set(omega_list)  # 转化为集合便于操作
    while len(omega_list) > 0:
        gama_old = copy.deepcopy(gama)
        j = random.choice(list(omega_list))  # 随机选取一个核心对象
        k = k + 1
        Q = list()
        Q.append(j)
        gama.remove(j)
        while len(Q) > 0:
            q = Q[0]
            Q.remove(q)
            if len(neighbor_list[q]) >= min_Pts:
                delta = neighbor_list[q] & gama
                deltalist = list(delta)
                for i in range(len(delta)):
                    Q.append(deltalist[i])
                    gama = gama - delta
        Ck = gama_old - gama
        Cklist = list(Ck)
        for i in range(len(Ck)):
            cluster[Cklist[i]] = k
        omega_list = omega_list - Ck
    return cluster
</code></pre> 

<p>2.制造一些数据</p>
<pre><code class="language-python"># 围成一个圈的数据
X1, y1 = datasets.make_circles(n_samples=2000, factor=.6, noise=.02)
# 生成零散的点
X2, y2 = datasets.make_blobs(n_samples=400, n_features=2, centers=[[1.2, 1.2]], cluster_std=[[.1]], random_state=9)
X = np.concatenate((X1, X2))
</code></pre>
<p>配置半径r值(eps)和r半径区域内最少数据点的个数</p>
<pre><code class="language-python">eps = 0.08
min_Pts = 10
</code></pre>

<p>3. 开始运行，大约耗时一分钟</p><pre><code class="language-python"></code>print("开始工作...")
begin = time.time()
C = DBSCAN(X, eps, min_Pts)
end = time.time()
print("用时", end - begin,"秒")
plt.figure()
plt.scatter(X[:, 0], X[:, 1], c=C)
plt.show()</code></pre>
<p>最后，会生成这样的聚类图</p>
<img src="../../images/lab04/dbscan-1.png" alt="">
<p>其中蓝色一类，黄色一类，这两个圈是datasets.make_circles生成的
<br>
右上角那一坨是datasets.make_blobs生成的
</p>
</div>
                </div>
                
              </div>
            </article>
          </div>
        </div>
      </div>
    </div>

    <div id="offcanvas-docs" data-uk-offcanvas="overlay: true">
      <div class="uk-offcanvas-bar">
        <button class="uk-offcanvas-close" type="button" data-uk-close></button>
        <h5 class="uk-margin-top">关于这门课</h5>
        <ul class="uk-nav uk-nav-default doc-nav">
          <li><a href="../indext.html">大纲</a></li>
        </ul>
        <h5 class="uk-margin-top">章节</h5>
        <ul class="uk-nav uk-nav-default doc-nav">
          <li><a href="../01.html">第1章 - 数据挖掘概念</a></li>
          <li><a href="../02.html">第2章 - 分类</a></li>
          <li><a href="../03.html">第3章 - 聚类</a></li>
          <li><a href="../04.html">第4章 - 关联规则</a></li>
          <li><a href="../05.html">第5章 - 日志的挖掘与应用</a></li>
          <li><a href="../06.html">第6章 - 数据挖掘应用案例</a></li>
        </ul>
        <h5>实验课</h5>
        <ul class="uk-nav uk-nav-default doc-nav">
          <li><a href="./code-01.html">01</a></li>
          <li><a href="./code-02.html">02</a></li>
          <li><a href="./code-03.html">03</a></li>
          <li class="uk-active"><a href="./code-04.html">04</a></li>
          <li><a href="./code-05.html">05</a></li>
          <li><a href="./code-06.html">06</a></li>
          <li><a href="./code-07.html">07</a></li>
          <li><a href="./code-08.html">08</a></li>
          <li><a href="./code-09.html">09</a></li>
          <li><a href="./code-10.html">10</a></li>
          <li><a href="./code-11.html">11</a></li>
          <li><a href="./code-12.html">12</a></li>
          <li><a href="./code-13.html">13</a></li>
          <li><a href="./code-14.html">14</a></li>
          <li><a href="./code-15.html">15</a></li>
          <li><a href="./code-16.html">16</a></li>
        </ul>
      </div>
    </div>

    <footer class="uk-section uk-text-center uk-text-muted">
      <div class="uk-container uk-container-small">
        <div class="uk-margin-medium"></div>
      </div>
    </footer>

    <script src="../js/awesomplete.js"></script>
    <script src="../js/custom.js"></script>
  </body>
</html>
